/** ****************************************************************************
 * CopyRight (C) 2012 Sharadendu Sinha.
 * Licensing : FreeBSD 
 *
 * @(#) $Id$
 */
package com.sps.signalling;

import com.sps.signalling.exceptions.SignallingException;

/**
 * This is an implementation of {@link InvokableConnection} interface
 * 
 * @version 1.1
 * @author sharadendu
 */
class InvokableConnectionImpl
	implements InvokableConnection
{
	/** {@link Signal} as returned from {@link #getSourceInvokable()} */
	private final Signal	sourceInvokable;
	/** {@link Invokable} as returned from {@link #getDestinationInvokable()} */
	private final Invokable	destinationInvokable;
	/** {@link Type} as returned from {@link #getType()} */
	private final Type		type;

	/**
	 * Only constructor for {@link InvokableConnection}
	 * 
	 * @param source {@link Signal} to which connection is made
	 * @param destination {@link Invokable}, can be a {@link Signal} or
	 *            {@link Slot}
	 * @param type {@link Type}
	 */
	public InvokableConnectionImpl(Signal source, Invokable destination,
		Type type)
	{
		if(source == null || destination == null)
		{
			throw new SignallingException(SignallingException.INVOKABLE_NULL);
		}
		this.sourceInvokable = source;
		this.destinationInvokable = destination;
		if(type == null)
		{
			this.type = Type.DIRECT;
		}
		else
		{
			this.type = type;
		}
	}

	/**
	 * @see com.sps.signalling.InvokableConnection#getSourceInvokable()
	 */
	@Override
	public Signal getSourceInvokable()
	{
		return sourceInvokable;
	}

	/**
	 * @see com.sps.signalling.InvokableConnection#getDestinationInvokable()
	 */
	@Override
	public Invokable getDestinationInvokable()
	{
		return destinationInvokable;
	}

	/**
	 * @see com.sps.signalling.InvokableConnection#getType()
	 */
	@Override
	public Type getType()
	{
		return type;
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj)
	{
		boolean equals = false;
		if(obj instanceof InvokableConnection)
		{
			InvokableConnection connection = (InvokableConnection)obj;
			equals = connection.getSourceInvokable().equals(sourceInvokable)
				&& connection.getDestinationInvokable().equals(
					destinationInvokable) && connection.getType() == type;
		}
		return equals;
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode()
	{
		return sourceInvokable.hashCode() | destinationInvokable.hashCode()
			| type.hashCode();
	}
}
